package com.findu.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.findu.R;
import com.example.findu.R.drawable;
import com.example.findu.R.id;
import com.example.findu.R.layout;
import com.findu.control.Public_title_dynamic;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;

/**
 * 
 * @ClassName: Home__relatives_information
 * @Description: TODO(亲友资料)
 * @author han
 * @date 2015-10-30 上午10:26:07
 * 
 */
public class Activity_Home__relatives_information extends Activity implements
		OnClickListener {
	// 声明title
	private Public_title_dynamic public_title;
	private View title_View;

	// 声明意图接收参数
	private Intent intent;

	// 存放传递过来的值
	private String Name;
	private String personalNote;

	// ----------------图片选择器
	private GridView gridView1; // 网格显示缩略图
	private Button buttonPublish; // 发布按钮
	private final int IMAGE_OPEN = 1; // 打开图片标记
	private String pathImage; // 选择图片路径
	private Bitmap bmp; // 导入临时图片
	private ArrayList<HashMap<String, Object>> imageItem;
	private SimpleAdapter simpleAdapter; // 适配器

	// -----------------页面下方添加亲属
	private ImageView image_Father, image_Mather, image_spouse, image_brother,
			image_children;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_home_relatives_information);

		// 初始化数据
		initData();

		// 运行图片添加器
		initMethod();

		// 页面下方添加亲属
		init_Add_Relatives();
		
		

	}

	/**
	 * 事件监听器
	 * 
	 * @param arg0
	 */
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		//初始化Intent
		if (intent != null) {
			intent = null;
		}
			intent = new Intent(Activity_Home__relatives_information.this,new Relatives_information_details_AddRelatives().getClass());
		
			//分别设置不同的titl名称
		switch (arg0.getId()) {
		case R.id.relatives_information_ImageView1:
			intent.putExtra("title_Name", "添加父亲");
			startActivity(intent);
			break;
		case R.id.relatives_information_ImageView2:
			intent.putExtra("title_Name", "添加母亲");
			startActivity(intent);
			break;
		case R.id.relatives_information_ImageView3:
			intent.putExtra("title_Name", "添加配偶");
			startActivity(intent);
			break;
		case R.id.relatives_information_ImageView4:
			intent.putExtra("title_Name", "添加兄弟姐妹");
			startActivity(intent);
			break;
		case R.id.relatives_information_ImageView5:
			intent.putExtra("title_Name", "添加子女");
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	/**
	 * 添加亲属跳转页
	 */
	private void init_Add_Relatives() {
		// TODO Auto-generated method stub

		// 初始化需要的控件
		image_Father = (ImageView) findViewById(R.id.relatives_information_ImageView1);
		image_Mather = (ImageView) findViewById(R.id.relatives_information_ImageView2);
		image_spouse = (ImageView) findViewById(R.id.relatives_information_ImageView3);
		image_brother = (ImageView) findViewById(R.id.relatives_information_ImageView4);
		image_children = (ImageView) findViewById(R.id.relatives_information_ImageView5);

		// 绑定监听器
		image_Father.setOnClickListener(this);
		image_Mather.setOnClickListener(this);
		image_spouse.setOnClickListener(this);
		image_brother.setOnClickListener(this);
		image_children.setOnClickListener(this);
	}

	/**
	 * 初始化数据
	 */
	private void initData() {

		// 接收传递过来的值
		intent = getIntent();
		Name = intent.getStringExtra("list_Name");
		personalNote = intent.getStringExtra("list_Explain");

		// 初始化title
		title_View = findViewById(R.id.activity_home_relative_information_include);

		public_title = new Public_title_dynamic(title_View);
		public_title.setTextView(Name);
		public_title.setTextRight("详细资料");
		public_title.setRighttImageVisiable(true);
		public_title.textView_left_Visible_Gone();

		// 设置title退出按钮
		ImageView leftImage_title = (ImageView) title_View
				.findViewById(R.id.public_title_dynamic__liftImage);
		leftImage_title.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		// 设置title右边详细资料
		TextView right_textview = (TextView) title_View
				.findViewById(R.id.public_title_dynamic_textViewright);
		right_textview.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intetn = new Intent(
						Activity_Home__relatives_information.this,
						new Relatives_information_details().getClass());
				intetn.putExtra("list_Name", Name);
				
				startActivity(intetn);

			}
		});
	}

	/**
	 * 添加图片选择器
	 */
	private void initMethod() {
		// TODO Auto-generated method stub
		/*
		 * 防止键盘挡住输入框 不希望遮挡设置activity属性 android:windowSoftInputMode="adjustPan"
		 * 希望动态调整高度 android:windowSoftInputMode="adjustResize"
		 */
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		/*
		 * //锁定屏幕
		 * setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		 * setContentView(R.layout.activity_main);
		 */
		// 获取控件对象
		gridView1 = (GridView) findViewById(R.id.gridView1);

		/*
		 * 载入默认图片添加图片加号 通过适配器实现 SimpleAdapter参数imageItem为数据源
		 * R.layout.griditem_addpic为布局
		 */
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.xi_zone2); // 加号
		imageItem = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("itemImage", bmp);
		imageItem.add(map);
		simpleAdapter = new SimpleAdapter(this, imageItem,
				R.layout.fragment_conversation_page2_include_griditem_addpic,
				new String[] { "itemImage" }, new int[] { R.id.imageView1 });
		/*
		 * HashMap载入bmp图片在GridView中不显示,但是如果载入资源ID能显示 如 map.put("itemImage",
		 * R.drawable.img); 解决方法: 1.自定义继承BaseAdapter实现 2.ViewBinder()接口实现 参考
		 * http://blog.csdn.net/admin_/article/details/7257901
		 */
		simpleAdapter.setViewBinder(new ViewBinder() {
			@Override
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				// TODO Auto-generated method stub
				if (view instanceof ImageView && data instanceof Bitmap) {
					ImageView i = (ImageView) view;
					i.setImageBitmap((Bitmap) data);
					return true;
				}
				return false;
			}
		});
		gridView1.setAdapter(simpleAdapter);

		/*
		 * 监听GridView点击事件 报错:该函数必须抽象方法 故需要手动导入import android.view.View;
		 */
		gridView1.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				if (imageItem.size() == 10) { // 第一张为默认图片
					Toast.makeText(Activity_Home__relatives_information.this,
							"图片数9张已满", Toast.LENGTH_SHORT).show();
				} else if (position == 0) { // 点击图片位置为+ 0对应0张图片
					Toast.makeText(Activity_Home__relatives_information.this,
							"添加图片", Toast.LENGTH_SHORT).show();
					// 选择图片
					Intent intent = new Intent(
							Intent.ACTION_PICK,
							android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
					startActivityForResult(intent, IMAGE_OPEN);
					// 通过onResume()刷新数据
				} else {
					dialog(position);
					// Toast.makeText(MainActivity.this, "点击第" + (position + 1)
					// + " 号图片",
					// Toast.LENGTH_SHORT).show();
				}

			}
		});
	}

	// 获取图片路径 响应startActivityForResult
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 打开图片
		if (resultCode == RESULT_OK && requestCode == IMAGE_OPEN) {
			Uri uri = data.getData();
			if (!TextUtils.isEmpty(uri.getAuthority())) {
				// 查询选择图片
				Cursor cursor = getContentResolver().query(uri,
						new String[] { MediaStore.Images.Media.DATA }, null,
						null, null);
				// 返回 没找到选择图片
				if (null == cursor) {
					return;
				}
				// 光标移动至开头 获取图片路径
				cursor.moveToFirst();
				pathImage = cursor.getString(cursor
						.getColumnIndex(MediaStore.Images.Media.DATA));
			}
		} // end if 打开图片
	}

	// 刷新图片
	@Override
	protected void onResume() {
		super.onResume();
		if (!TextUtils.isEmpty(pathImage)) {
			Bitmap addbmp = BitmapFactory.decodeFile(pathImage);
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", addbmp);
			imageItem.add(map);
			simpleAdapter = new SimpleAdapter(
					this,
					imageItem,
					R.layout.fragment_conversation_page2_include_griditem_addpic,
					new String[] { "itemImage" }, new int[] { R.id.imageView1 });
			simpleAdapter.setViewBinder(new ViewBinder() {
				@Override
				public boolean setViewValue(View view, Object data,
						String textRepresentation) {
					// TODO Auto-generated method stub
					if (view instanceof ImageView && data instanceof Bitmap) {
						ImageView i = (ImageView) view;
						i.setImageBitmap((Bitmap) data);
						return true;
					}
					return false;
				}
			});
			gridView1.setAdapter(simpleAdapter);
			simpleAdapter.notifyDataSetChanged();
			// 刷新后释放防止手机休眠后自动添加
			pathImage = null;
		}
	}

	/*
	 * Dialog对话框提示用户删除操作 position为删除图片位置
	 */
	protected void dialog(final int position) {
		AlertDialog.Builder builder = new Builder(
				Activity_Home__relatives_information.this);
		builder.setMessage("确认移除已添加图片吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				imageItem.remove(position);
				simpleAdapter.notifyDataSetChanged();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	

}
